#!/bin/bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

cmd=$( basename "$0" )

caffeName='caffe'

function usage() {
	echo "Usage: ${cmd} [ -t | -h ]"
    echo -e "\t-t: 增加多src/gtest代码的索引"
    echo -e "\t-h: 显示帮助信息"
	exit 1
}

# 包括.c,.cc文件所在目录
srcDirs=( onnx )
# 包含.h文件所在目录
hppDirs=( onnx )

# 默认不索引test目录下的文件
findTestOpt="-name test -type d -prune -o"
while getopts "th" args
do
    case "${args}" in 
        t)
            findTestOpt=""
            ;;
        *)
            usage;;
    esac
done

root="."
for d in "${srdDirs[@]}"
do
    dir_exists_or_die "${root}/$d"
done

csFile="${root}/cscope.files"
echo "Build ${csFile} ..."
# 重置文件列表
echo > "${csFile}"
for d in "${hppDirs[@]}"
do
    find "${root}/$d" ${findTestOpt} -name "*.hpp" -type f -print >> "${csFile}"
    find "${root}/$d" ${findTestOpt} -name "*.h" -type f -print >> "${csFile}"
    find "${root}/$d" ${findTestOpt} -name "*.cuh" -type f -print >> "${csFile}"
done
for d in "${srcDirs[@]}"
do
    find "${root}/$d" ${findTestOpt} -name "*.cpp" -type f -print >> "${csFile}"
    find "${root}/$d" ${findTestOpt} -name "*.c" -type f -print >> "${csFile}"
    find "${root}/$d" ${findTestOpt} -name "*.cc" -type f -print >> "${csFile}"
    find "${root}/$d" ${findTestOpt} -name "*.cu" -type f -print >> "${csFile}"
    # 包括proto文件
    find "${root}/$d" ${findTestOpt} -name "*.proto" -type f -print >> "${csFile}"
    # 包括python文件
    find "${root}/$d" ${findTestOpt} -name "*.py" -type f -print >> "${csFile}"
done

# 建立cscope数据库
echo "Build cscope DB..."
cscope -Rbqk
echo "Done."
